package com.tzxs.pay.main.gather;

import android.app.Activity;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.Menu;

import com.tzxs.pay.R;
import com.tzxs.pay.BR;

import com.tzxs.pay.databinding.ItemBillCollectionBinding;
import com.tzxs.pay.main.base.BaseActivity;
import com.tzxs.pay.databinding.ActivityGatherListBinding;
import com.tzxs.pay.main.bill.detail.CollAndInstallDetailActivity;
import com.tzxs.pay.main.bill.model.CollectionBillBean;
import com.tzxs.pay.main.bill.viewmodel.BillViewModel;
import com.tzxs.pay.util.IAdapter;
import com.tzxs.pay.util.IHolder;
import com.tzxs.pay.util.StatusBarColorSetter;

import java.util.List;

//收款列表
public class GatherListActivity extends BaseActivity<ActivityGatherListBinding> implements IAdapter.ItemNavigator<CollectionBillBean> {
    private static final String TAG = GatherListActivity.class.getSimpleName();
    private BillViewModel model;
    private int page = 1;//默认1
    private int number = 10;//默认10 每页的数量

    @Override
    protected Activity getChildActivity() {
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_gather_list;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarColorSetter.setStatusBarColor(this, Color.parseColor("#FAFAFA"));
        model = ViewModelProviders.of(this).get(BillViewModel.class);
        dataBinding.gatherToolbar.setTitle("");
        setSupportActionBar(dataBinding.gatherToolbar);
        dataBinding.gatherToolbar.setNavigationOnClickListener(v -> onBackPressed());
        dataBinding.gatherToolbar.setOnMenuItemClickListener(item -> {
            if (item.getItemId() == R.id.toolbar_menu_search) {
                //跳转收款账单查询页面
                Intent intent = new Intent(GatherListActivity.this, SearchGaActivity.class);
                startActivity(intent);
            }

            return false;
        });

        dataBinding.gatherRec.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.gatherRec.setAdapter(new IAdapter<CollectionBillBean, ItemBillCollectionBinding>() {
            @Override
            protected int getLayoutId(int viewType) {
                return R.layout.item_bill_collection;
            }

            @Override
            protected int getDataBRId(int itemViewType) {
                return BR.item;
            }

            @Override
            public boolean areItemsTheSame(List<CollectionBillBean> oldData, List<CollectionBillBean> data, int oldItemPosition, int newItemPosition) {
                return oldData.get(oldItemPosition).getOut_trade_no().equals(data.get(newItemPosition).getOut_trade_no());
            }

            @Override
            public boolean areContentsTheSame(List<CollectionBillBean> oldData, List<CollectionBillBean> data, int oldItemPosition, int newItemPosition) {
                return oldData.get(oldItemPosition).getPay_status() == (data.get(newItemPosition).getPay_status());
            }

            @Override
            protected void convertListener(@NonNull ItemBillCollectionBinding binding) {
                super.convertListener(binding);
                binding.setItemNavigator(GatherListActivity.this);
            }

            @Override
            public void onBindViewHolder(IHolder<ItemBillCollectionBinding> holder, int position) {
                super.onBindViewHolder(holder, position);
                CollectionBillBean bean = getData().get(position);
                holder.binding.collectionItemTradeState.setText(bean.getPay_status_txt());

                //支付方式
                switch (bean.getPay_type()) {
                    case "weixin":
                        holder.binding.collectionWechatIcon.setImageResource(R.drawable.ic_wechat);
                        break;
                    case "alipay":
                        holder.binding.collectionWechatIcon.setImageResource(R.drawable.ic_alipay);
                        break;
                    default:
                        holder.binding.collectionWechatIcon.setImageResource(R.drawable.ic_unionpay);
                        break;
                }
            }
        });

        dataBinding.setViewModel(model);
        //获取到收款列表
        model.getCollectionBills(() -> {

        }, null, "all", page, number, null, null, null, null);


        //刷新数据
        dataBinding.refresh.setOnRefreshListener(refreshLayout -> {
            //下拉刷新重制page
            page = 1;
            //重新加载数据
            model.getCollectionBills(() -> {
                //延时2s刷新数据
                refreshLayout.finishRefresh(1000);
            }, null, "all", page, number, null, null, null, null);


        });


        //加载更多
        dataBinding.refresh.setOnLoadMoreListener(refreshLayout -> {

            ++page;
            Log.d(TAG, "viewCreate: 此时的page:" + page);
            model.getCollectionBills(() -> {
                refreshLayout.finishLoadMore(1000);
            }, null, "all", page, number, null, null, null, null);


        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search, menu);
        return true;
    }

    //item  单击事件

    @Override
    public void itemClick(CollectionBillBean item) {
        Intent intent = new Intent(this, CollAndInstallDetailActivity.class);
        intent.putExtra("orderId", item.getOut_trade_no());
        startActivity(intent);
    }
}
